<?php
/**
 * Created by PhpStorm.
 * Register: czm
 * Date: 2019/4/24
 * Time: 11:09
 */
namespace app\admin\controller;

use think\Controller;
use think\Db;

class Commodity extends Controller
{
    public function commodityCategoryList()
    {
        $list = Db::name('commodity_category')->order('id desc')->select();
//        $page = $list->render();
//        $this->assign('page', $page);
        $this->assign('data', $list);
        return $this->fetch();
    }

    public function commodityCategoryAdd()
    {
        if (request()->isPost()){
            $data = [
                'name' => input('name'),
            ];
            var_dump($data);exit();

            $res = Db::name('commodity_category')->insert($data);
            if ($res){
                return show(200);
            }else{
                return show(403);
            }
        }
        return $this->fetch();
    }

    public function commodityCategoryEdit()
    {
        $id = input('id');
        $admin = db('commodity_category')->find($id);
        $this->assign('a', $admin);
        if (request()->isPost()) {
            $data = input('post.');
            $res = Db::name('commodity_category')->where('id',input('id'))->update($data);
            if ($res){
                return show(200);
            }else{
                return show(403);
            }
        }
        return $this->fetch();
    }


    public function commodityCategoryDel()
    {
        $id = $_POST['id'];
        $data = db('commodity_category')->where('id',$id)->delete($id);
        if($data){
            return show(200);
        }else{
            return show(403);
        }
    }

    public function commodityList()
    {
        $list = Db::name('commodity')->order('id desc')->select();
        $this->assign('data', $list);
        $c = db('commodity_category')->select();
        $this->assign('c',$c);
        return $this->fetch();

    }

    public function commodityAdd()
    {
        $a = db('commodity_category')->select();
        $this->assign('a',$a);
        $name = input('role');
        if (request()->isPost())
        {
            $size = 2567800;
            //4. 判断上传的文件是否为允许大小
            if($_FILES['pic']['size'] > $size ) {
                return ("超过了允许的<b>2M</b>大小");
            }
            if ($_FILES['pic']['tmp_name'])
            {
                $file=request()->file('pic');//文件上传
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/images');//移动文件，DS代表/或者\斜杠
                $da=$info->getSaveName();//图片的保存路径
            }
            $data = [
                'eid'   =>  $name,
                'name'  =>  input('name'),
                'pic'   =>  $da,
                'price' =>  input('price'),
                'place' =>  input('place'),
                'specs' =>  input('specs'),
                'storage' =>  input('storage'),
                'nutritive'=>   input('nutritive'),
                'content'   =>  input('content'),
                'state' =>  input('state'),
            ];
            $res = Db::name('commodity')->insert($data);
            if ($res)
            {
                return show(200);
            }else{
                return show(403);
            }
        }
        return $this->fetch();
    }

    public function commodityEdit()
    {
        $id = input('id');
        $admin = db('commodity')->find($id);
        $this->assign('a', $admin);
        if (request()->isPost())
        {
            if ($_FILES['pic']['tmp_name'])
            {
                $file = request()->file('pic');//文件上传
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/images');//移动文件，DS代表/或者\斜杠
                $da = $info->getSaveName();//图片的保存路径
            }
            $data =[
                'name'  =>input('name'),
                'pic'   =>  $da,
                'price' =>  input('price'),
                'place' =>  input('place'),
                'specs' =>  input('specs'),
                'storage' =>  input('storage'),
                'nutritive'=>   input('nutritive'),
                'content'   =>input('content'),
            ];
            $res = Db::name('commodity')->where('id',$id)->update($data);
            if ($res){
                return show(200);
            }else{
                return show(403);
            }
        }
        return $this->fetch();
    }

    //上架下架
    public function commodityActive()
    {
        // 0上架状态  1下架状态
        $id = $_POST['id'];
        $active = $_POST['state'];
//        var_dump($id);exit();
        if($active == 0){
            db('commodity')->where('id',$id)->update(['state'=>0]);
            return show(200);
//            $this->success('上架成功');
        }else if ($active == 1){
            db('commodity')->where('id',$id)->update(['state'=>1]);
            return show(403);
//            $this->error('下架成功');
        }
    }

    public function commodityDel()
    {
        $id = $_POST['id'];
        $data = db('commodity')->where('id',$id)->delete($id);
        if($data){
            return show(200);
        }else{
            return show(403);
        }
    }
}